﻿using System;
using UnityEngine;
using System.Collections;
using Random = UnityEngine.Random;

public class GameController : MonoBehaviour
{
    private GridScript[,] _gridMap;
    public GameObject Grid;
    private GameObject[,] _maps;

    void Awake()
    {
        InitGridMap();
        InitMap(_gridMap);
    }

    void InitMap(GridScript[,] mGridMap)
    {
        _maps = new GameObject[mGridMap.GetLength(0), mGridMap.GetLength(1)];
        for (int y = 0; y < mGridMap.GetLength(0); y++)
        {
            for (int x = 0; x < mGridMap.GetLength(1); x++)
            {
                GameObject tempGrid =
                    (GameObject) Instantiate(Grid, new Vector3(x, 0, y), Quaternion.Euler(new Vector3(90, 0, 0)));
                switch (mGridMap[x, y].gridType)
                {
                    case GridType.None:
                        break;
                    case GridType.Road:
                        break;
                    case GridType.Obstacle:
                        tempGrid.renderer.material.color = Color.black;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                _maps[x, y] = tempGrid;
            }
        }
        GameObject startPoint = GameObject.CreatePrimitive(PrimitiveType.Cube);
        startPoint.transform.position = new Vector3(0, 0.5f, 0); 
        GameObject endPoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        endPoint.transform.position = new Vector3(9, 0.5f, 9);

        AStar aStar = new AStar(_gridMap, _gridMap[0, 0], _gridMap[1, 1]);
        aStar.FindWay();
        aStar.Print();
    }

    void InitGridMap()
    {
        _gridMap = new GridScript[10, 10];
        for (int y = 0; y < 10; y++)
        {
            for (int x = 0; x < 10; x++)
            {
                _gridMap[x, y] = new GridScript(x, y) {gridType = (GridType) Random.Range(0, 3)};
            }
        }
    }
}
